﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using MasApp;

namespace MasApp.BLL
{
    /// <summary>
    ///reg 的摘要说明
    /// </summary>
    public class reg
    {
        public reg()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 插入结果集，elementid, elementtype, elementname, levelvalue, weightvalue, projectid
        /// </summary>
        /// <param name="res"></param>
        /// <returns></returns>
        public static bool InsertResult(Model.result res)
        {
            string s = "Insert Into result(elementid, elementtype, elementname, levelvalue, weightvalue, projectid)"
                + " Values(" + res.Elementid + ",'" + res.Elementtype + "',"
                + res.Levelvalue + "," + res.Weightvalue
                + "," + res.Projectid + ")";
            return DAL.reg.ExecuteNonQuery(s) > 0 ? true : false;
        }
        /// <summary>
        /// aio.Indicatorid + aio.Indicatororder+ aio.Weightmissionid
        /// </summary>
        /// <param name="aio"></param>
        /// <returns></returns>
        public static bool InsertAppIndicatorOrder(Model.appindicatororder aio)
        {
            string s = "Insert INto appindicatororder(indicatorid,"
                + " indicatororder, weightmissionid) Values("
                + aio.Indicatorid + ","
                + aio.Indicatororder + ","
                + aio.Weightmissionid + ")";
            return DAL.reg.ExecuteNonQuery(s) > 0 ? true : false;
        }
        /// <summary>
        /// aro.Ruleid++aro.Ruleorder+aro.Weightmissionid
        /// </summary>
        /// <param name="aro"></param>
        /// <returns></returns>
        public static bool InsertAppRuleOrder(Model.appruleorder aro)
        {
            string s="Insert INto appruleorder(ruleid,"
                +" ruleorder, weightmissionid) Values("
                +aro.Ruleid+","
                +aro.Ruleorder+","
                +aro.Weightmissionid+")";
            return DAL.reg.ExecuteNonQuery(s) > 0 ? true : false;
        }
        /// <summary>
        /// 插入评级记录返回评级ID  -appl.Levelmissionid + appl.Indicatorid + appl.Levelid
        /// </summary>
        /// <param name="appl"></param>
        /// <returns></returns>
        public static int InsertAppLevel(Model.applevel appl)
        {
            return MasApp.DAL.reg.InsertAppLevel(appl);
        }
        // 这里的方法返回 int 类型（即相应的 ID 值）
        /// <summary>
        /// 插入用户角色ur.Userid + ur.Roleid
        /// </summary>
        /// <param name="ur"></param>
        /// <returns></returns>
        public static int RegUserRole(MasApp.Model.userrole ur)
        {
            return MasApp.DAL.reg.InsertUserRole(ur);
        }
        /// <summary>
        /// 插入等级任务Actor +Actorroleid + Projectid +Type + State
        /// </summary>
        /// <param name="lm"> </param>
        /// <returns></returns>
        public static int RegLevelMission(Model.levelmission lm)
        {
            return MasApp.DAL.reg.InsertLevelMission(lm);
        }
        /// <summary>
        /// 插入项目Projectname + Creator +  Modelid + Methodid + Starttime + Endtime + Description + Organizationid + Weighttype
        /// </summary>
        /// <param name="pro"> + </param>
        /// <returns></returns>
        public static int RegProject(Model.project pro)
        {
            return MasApp.DAL.reg.InsertProject(pro);
        }
        /// <summary>
        /// 插入指标Indicatorname + Ruleid + Description +
        /// </summary>
        /// <param name="indi"> </param>
        /// <returns></returns>
        public static int RegIndicator(Model.indicator indi)
        {
            return MasApp.DAL.reg.InsertIndicator(indi);
        }
        /// <summary>
        /// 插入用户Username + Realname + Sex + Email + Mphone + Phone + Address + Postcode + Organizationid + Description
        /// </summary>
        /// <param name="users"> </param>
        /// <returns></returns>
        public static int RegUsers(MasApp.Model.users users)
        {
            return MasApp.DAL.reg.InsertUsers(users);
        }
        /// <summary>
        /// 插入组织Creator +Organizationname +Address + Postcode + Phone +Contactperson + Contactemail + Description
        /// </summary>
        /// <param name="org">. </param>
        /// <returns></returns>
        public static int RegOrganization(MasApp.Model.organization org)
        {
            return MasApp.DAL.reg.InsertOrg(org);
        }
        /// <summary>
        /// 插入权值任务返回 weightmissionid
        /// ----Weightmissionname +Weightmissiontype +Actor +Projectid +State +Description
        /// </summary>
        /// <param name="wm"></param>
        /// <returns></returns>
        public static int RegWeightMission(Model.weightmission wm)
        {
            return DAL.reg.InsertWeightMission(wm);
        }
        /// <summary>
        /// 插入权重，返回 weightid-----Elementid +Elementtype Weightvalue +Weightmissionid
        /// </summary>
        /// <param name="we"></param>
        /// <returns></returns>
        public static int RegWeight(Model.weight we)
        {
            return DAL.reg.InsertWeight(we);
        }
        //---------------------------------------------------
        /// <summary>
        /// 添加序关系法评权专家--Weightmissionname +Weightmissiontype +Actor +Projectid +State +Description---ur.Userid + ur.Roleid
        /// </summary>
        /// <param name="wm"></param>
        /// <param name="ur"></param>
        /// <returns></returns>
        public static int AddWeightExpert(Model.weightmission wm, Model.userrole ur)
        {
            ur.Userroleid = MasApp.BLL.reg.RegUserRole(ur);
            if (ur.Userroleid > 0)
            {
                wm.Weightmissionname = ur.Userroleid.ToString();
                wm.Weightmissionid =RegWeightMission(wm);
                if (wm.Weightmissionid > 0)
                    return wm.Weightmissionid;
                else
                    return 0;
            }
            else
                return 0;
        }
        /// <summary>
        /// 添加评级专家
        /// </summary>
        /// <param name="lm">Actor +Actorroleid + Projectid +Type + State </param>
        /// <param name="ur">Userid + ur.Roleid</param>
        /// <returns></returns>
        public static int AddExpert(Model.levelmission lm, Model.userrole ur)
        {
            if (MasApp.DAL.Search.HaveUserInLevelMission(lm) > 0)
            {
                return 1;
            }
            else
            {
                ur.Userroleid = MasApp.BLL.reg.RegUserRole(ur);
                if (ur.Userroleid > 0)
                {
                    lm.Actorroleid = ur.Userroleid;
                    lm.Levelmissionid = RegLevelMission(lm);
                    if (lm.Levelmissionid > 0)
                        return lm.Levelmissionid;
                    else
                        return 0;
                }
                else
                    return 0;
            }
        }
        /// <summary>
        /// 添加登录信息
        /// </summary>
        /// <param name="log">username,password</param>
        /// <returns></returns>
        public static int RegLogin(MasApp.Model.login log)
        {
            return MasApp.DAL.reg.InsertLogin(log);
        }

        // 注册方法返回 bool 类型
        /// <summary>
        /// 
        /// </summary>
        /// <param name="log">username,password</param>
        /// <param name="us">Userid + ur.Roleid</param>
        /// <returns></returns>
        public static bool InsertUsLog(Model.login log, Model.users us)
        {

            int k = reg.RegUsers(us);//一定要先regUser或者regLogin
            int i = reg.RegLogin(log);
            if (i > 0  && k > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 插入角色
        /// </summary>
        /// <param name="log">username,password</param>
        /// <param name="us">Username + Realname + Sex + Email + Mphone + Phone + Address + Postcode + Organizationid + Description</param>
        /// <param name="ur">Userid + ur.Roleid</param>
        /// <returns></returns>
        public static bool AddRole(Model.login log, Model.users us, Model.userrole ur)
        {
            int k = reg.RegUsers(us);//一定要先regUser或者regLogin
            int i = reg.RegLogin(log);

            ur.Userid = Search.SearchUserID(log.Username);
            int j = reg.RegUserRole(ur);
            if (i > 0 && j > 0 && k > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 插入角色4
        /// </summary>
        /// <param name="log">username,password</param>
        /// <param name="us">Username + Realname + Sex + Email + Mphone + Phone + Address + Postcode + Organizationid + Description</param>
        /// <param name="ur">Userid + ur.Roleid</param>
        /// <returns></returns>
        public static bool AddRole4(Model.login log, Model.users us, Model.userrole ur)
        {
            int k = reg.RegUsers(us);//一定要先regUser或者regLogin
            int i = reg.RegLogin(log);

            ur.Userid = Search.SearchUserID(log.Username);
            int j = reg.RegUserRole(ur);
            if (i > 0 && j > 0 && k > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 插入系统管理员用户
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="realname"></param>
        /// <param name="mphone"></param>
        /// <returns></returns>
        public static bool AddmanaUser(string username, string password, string realname, string mphone)
        {
            try
            {
                #region 插入登录信息
                MasApp.Model.login log = new MasApp.Model.login();
                log.Username = username;
                log.Password = password;
                int i = MasApp.BLL.reg.RegLogin(log);
                #endregion

                #region 插入用户表
                MasApp.Model.users us = new MasApp.Model.users();
                us.Username = username;
                us.Realname = realname;
                us.Mphone = mphone;
                int j = MasApp.BLL.reg.RegUsers(us);
                #endregion

                #region 插入用户角色表
                MasApp.Model.userrole ur = new MasApp.Model.userrole();
                ur.Userid = MasApp.BLL.Search.SearchUserID(username);
                ur.Roleid = 2;
                int k = MasApp.BLL.reg.RegUserRole(ur);
                #endregion
                if (i > 0 && j > 0 && k > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
                //System.Web.HttpContext.Current.Response.Write(ee.ToString());
                return false;
            }
        }
        /// <summary>
        /// 插入方法
        /// </summary>
        /// <param name="method">Methodname;Creator;Description;.Organizationid;</param>
        /// <returns></returns>
        public static bool AddMethod(Model.method method)
        {
            return MasApp.DAL.reg.InsertMethod(method) > 0 ? true : false;
        }
        /// <summary>
        /// 插入等级
        /// </summary>
        /// <param name="lev">.Levelname;.Judgement;.Methodid;Levelvalue;</param>
        /// <returns></returns>
        public static bool AddLevel(Model.level lev)
        {
            return MasApp.DAL.reg.InsertLevel(lev) > 0 ? true : false;
        }
        /// <summary>
        /// 插入模型返回bool
        /// </summary>
        /// <param name="mod">.Modelname;Creator;.Target;.Description;.Organizationid;</param>
        /// <returns></returns>
        public static bool RegModelbool(MasApp.Model.model mod)
        {
            return MasApp.DAL.reg.InsertModel(mod) > 0 ? true : false;
        }
        /// <summary>
        /// 插入用户角色返回bool
        /// </summary>
        /// <param name="ur">Userid + ur.Roleid</param>
        /// <returns></returns>
        public static bool RegUserRolebool(MasApp.Model.userrole ur)
        {
            return MasApp.DAL.reg.InsertUserRole(ur) > 0 ? true : false;
        }
        /// <summary>
        /// 插入用户返回bool
        /// </summary>
        /// <param name="users">Username + Realname + Sex + Email + Mphone + Phone + Address + Postcode + Organizationid + Description</param>
        /// <returns></returns>
        public static bool RegUsersbool(MasApp.Model.users users)
        {
            return MasApp.DAL.reg.InsertUsers(users) > 0 ? true : false;
        }
        /// <summary>
        /// 插入组织返回bool
        /// </summary>
        /// <param name="org">Creator +Organizationname Address +Postcode +Phone +Contactperson + Contactemail + Description</param>
        /// <returns></returns>
        public static bool RegOrganizationbool(MasApp.Model.organization org)
        {
            return MasApp.DAL.reg.InsertOrg(org) > 0 ? true : false;
        }
        /// <summary>
        /// 插入登录返回bool
        /// </summary>
        /// <param name="log">username ，password</param>
        /// <returns></returns>
        public static bool RegLoginbool(MasApp.Model.login log)
        {
            return MasApp.DAL.reg.InsertLogin(log) > 0 ? true : false;
        }
        /// <summary>
        /// 插入准则返回bool
        /// </summary>
        /// <param name="rule">.Rulename;Description;.Modelid;</param>
        /// <returns></returns>
        public static bool AddRule(Model.rule rule)
        {
            return MasApp.DAL.reg.InsertRule(rule) > 0 ? true : false;
        }
        /// <summary>
        /// 插入指标返回bool
        /// </summary>
        /// <param name="indi">indi.Indicatorname +Description +Ruleid +</param>
        /// <returns></returns>
        public static bool AddIndicator(Model.indicator indi)
        {
            return MasApp.DAL.reg.InsertIndicator(indi) > 0 ? true : false;
        }
        /// <summary>
        /// 插入项目
        /// </summary>
        /// <param name="pro">Projectname +Creator +Modelid +Methodid +.Starttime + ndtime +Description +Organizationid +Weighttype +</param>
        /// <returns></returns>
        public static bool AddProject(Model.project pro)
        {
            return MasApp.DAL.reg.InsertProject(pro) > 0 ? true : false;
        }


        //
    }
}